package com.ruoyi.business.service.product.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.product.TbProductSkus;
import com.ruoyi.business.mapper.product.TbProductSkusMapper;
import com.ruoyi.business.service.product.TbProductSkusService;
import com.ruoyi.common.core.domain.R;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 商品SKU表Service实现类
 */
@Service
public class TbProductSkusServiceImpl extends ServiceImpl<TbProductSkusMapper, TbProductSkus>
        implements TbProductSkusService {

    @Override
    public List<TbProductSkus> selectSkusList(Long productId) {
        LambdaQueryWrapper<TbProductSkus> eq = new LambdaQueryWrapper<TbProductSkus>().eq(TbProductSkus::getProductId, productId);
        return this.list(eq);
    }

    @Override
    public TbProductSkus selectSkusById(Long id) {
        return this.getById(id);
    }

    @Override
    public R<Boolean> insertSkus(TbProductSkus skus) {
        LambdaQueryWrapper<TbProductSkus> or = new LambdaQueryWrapper<TbProductSkus>().eq(TbProductSkus::getBarcode, skus.getBarcode())
            .or(wrapper -> wrapper.eq(TbProductSkus::getSkuCode, skus.getSkuCode()));
        if (this.count(or) > 0){
            return R.fail( "商品规格编码或条码已存在");
        }
        skus.setSpecsText(TbProductSkus.getSpecsText(skus.getSpecs()));
        return R.ok(this.save(skus));
    }

    @Override
    public boolean updateSkus(TbProductSkus skus) {
        skus.setSpecsText(TbProductSkus.getSpecsText(skus.getSpecs()));
        return this.updateById(skus);
    }

    @Override
    public boolean deleteSkusById(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean deleteSkusByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList((ids)));
    }
}
